package com.mall.web.control.app.home;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.home.HomeBizImpl;
import com.mall.common.util.PageUtil;
import com.mall.pojo.MallBanner;
import com.mall.query.MallGroupBuyQuery;
import com.mall.query.Page;
import com.mall.vo.home.MallExtensionVO;
import com.mall.vo.home.MallGroupBuyVO;
import com.mall.web.annotation.CheckCommmonOnlyKey;
import com.mall.web.util.HttpUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/home/")
public class HomeController {

	@Autowired
	private HomeBizImpl homeBiz;

	/**
	 * 首页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("index")
	@CheckCommmonOnlyKey
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		indexMap(request,response,modelMap);
		return "home/index";

	}
	@RequestMapping("indexForApp")
	@ResponseBody
	public ResponseEntity<ModelMap> indexForApp(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		indexMap(request,response,modelMap);
		ResponseEntity<ModelMap> responseEntity = new ResponseEntity<>();
		responseEntity.initSuccResponseEntity(modelMap, "index message");
		return responseEntity;
		
	}
	
	@RequestMapping("indexMoreList")
	@CheckCommmonOnlyKey
	public String indexMoreList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		indexMap(request,response,modelMap);
		return "home/more/indexMoreList";

	}
	
	
	
	private ModelMap indexMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);

		String keyWord = HttpUtil.getURLDecoderValueFromRequest(request, "keyWord");
		Integer categoryId = HttpUtil.getIntegerValueFromRequest(request, "categoryId");
		Integer secCategoryId = HttpUtil.getIntegerValueFromRequest(request, "secCategoryId");
		
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_MALL);
		Page page = new Page();
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		MallGroupBuyQuery mallGroupBuyQuery = new MallGroupBuyQuery();
		mallGroupBuyQuery.setOnlyKey(onlyKey);
		mallGroupBuyQuery.setCategoryId(categoryId);
		mallGroupBuyQuery.setSecCategoryId(secCategoryId);
		mallGroupBuyQuery.setKeyWord(keyWord);
		mallGroupBuyQuery.setPage(page);
		mallGroupBuyQuery.setDoset(true);
		//首页下面的团购内容
		List<MallGroupBuyVO> mallGroupBuyVOList = this.homeBiz.getMallGroupBuyVOByQuery(mallGroupBuyQuery);
		
		// 首页店铺轮播MallBanner.type=0
		List<MallBanner> mallBannerList = this.homeBiz.getMallBannerByOnlyKey(onlyKey);

		// 首页广告位 MallBanner.type=2
		List<MallBanner> mallBannerGGList = this.homeBiz.getMallBannerGGByOnlyKey(onlyKey);
		MallBanner mallBannerGG = null;
		if (mallBannerGGList != null && mallBannerGGList.size() > 0) {
			mallBannerGG = mallBannerGGList.get(0);
		}

		// 首页特色推荐MallExtension.type=0
		List<MallExtensionVO> mallExtensionVOList  = this.homeBiz.getMallExtensionVOListByOnlyKeyAndType(onlyKey,0);
		
		// 为你优选MallExtension.type=1
		List<MallExtensionVO> mallExtensionYXVOList = this.homeBiz.getMallExtensionVOListByOnlyKeyAndType(onlyKey,1);
		
		
		modelMap.put("mallBannerList", mallBannerList);
		modelMap.put("mallBannerGGList", mallBannerGGList);
		modelMap.put("mallBannerGG", mallBannerGG);
		modelMap.put("mallExtensionVOList", mallExtensionVOList);
		modelMap.put("mallExtensionYXVOList", mallExtensionYXVOList);
		modelMap.put("mallGroupBuyVOList", mallGroupBuyVOList);
		
		return modelMap;
		
		
	}

}
